export function formatdate(data, t) {
  
  try {
    if (typeof data == 'undefined' || !data) {
      return '';
    } else if (typeof data == 'string') {
      // data = new Date(data);
      data=new Date(data.replace(/-/g,'/'));//兼容ios时间
    }
    var d = data;
    var year = d.getFullYear();
    var months = d.getMonth() + 1;
    var month = months < 10 ? '0' + months : '' + months;
    var day = d.getDate() < 10 ? '0' + d.getDate() : '' + d.getDate();
    var hh = d.getHours();
    var mm = d.getMinutes();
    const ss = d.getSeconds();
    var h = d.getHours() < 10 ? '0' + d.getHours() : '' + d.getHours();
    var m = d.getMinutes() < 10 ? '0' + d.getMinutes() : '' + d.getMinutes();
    var s = d.getSeconds() < 10 ? '0' + d.getSeconds() : '' + d.getSeconds();

    if (t == 1) {
      return year + '-' + month + '-' + day;
    } else if (t == 2) {
      return year + '-' + month + '-' + day + ' ' + h + ':' + m;
    } else if (t == 3) {
      return hh + ':' + mm + ':' + ss;
    } else if (t == 4) {
      return year + '-' + month + '-' + day + ' ' + h + ':' + m + ':' + s;
    } else if (t==5){
      return year;
    } else {
      return year + '-' + month + '-' + day + ' ' + hh + ':' + mm + ':' + ss;
    }
  } catch (e) {
    console.error(e, 'formatdate exception');
  }
}


export function compare(date1,date2) {
	let dates1 = new Date(date1);
	let dates2 = new Date(date2);
	if (dates1 > dates2) {
		return true
	} else {
		return false
  }
}

// 根据日期获取日期前 T个月的日期 （data格式yyyy-mm-dd,t数字）
export function getPreMonthDay(date, t) {
  date=new Date(data.replace(/-/g,'/'));//兼容ios时间
  try {
    var dateArr = date.split('-');
    var year = dateArr[0]; //获取当前日期的年份
    var month = dateArr[1]; //获取当前日期的月份
    var day = dateArr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2 = parseInt(month) - t;
    if (month2 <= 0) {
      year2 = parseInt(year2) - parseInt(month2 / 12 == 0 ? 1 : parseInt(month2) / 12);
      month2 = 12 - (Math.abs(month2) % 12);
    }
    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
      day2 = days2;
    }
    if (month2 < 10) {
      month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
  } catch (e) {
    console.error(e, 'formatdate exception');
  }
}

